# 文件写入
# mode='w'  清空文件写入  
# mode='a'  追加写入
# mode='w+'，mode='a+'   读写
f = open("./8.io/demo_GBK.txt", mode="w", encoding="GBK")
f.write("人生苦短， 我用Python")
f.close()

# 文件读取
# mode='r'
f = open("./8.io/demo_GBK.txt", mode="r", encoding="GBK")
data = f.readlines() # list[str]
print(data)
f.close()

# 读取文件
f = open("./8.io/demo.txt", mode="r", encoding="UTF-8")
data = f.read() # str
print(data)
f.close()

# 迭代读取每行数据（常用）
# with 代码块执行完毕,对象自动关闭，类似C#  using，同时可以简化异常处理
with open("./8.io/demo.txt", mode="r", encoding="UTF-8") as f:
    for line in f:
        print(data)
